﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using TransactionServer.General_Classes;
using System.ComponentModel;
using Microsoft.VisualBasic.Activities;

namespace TransactionServer.CRM4Activities
{
    [Designer(typeof(TransactionServer.ActivityDesign.CRMActivities.CRMDelete))]
    public sealed class CRM4Delete : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<Context> Context { get; set; }
        public InArgument<Entity> Entity { get; set; }

        public CRM4Delete()
        {
            VisualBasicValue<Context> vbArgument = new VisualBasicValue<Context>(Constants.CONTEXT_NAME);
            this.Context = InArgument<Context>.FromExpression(vbArgument);
        }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            Entity Entity = context.GetValue(this.Entity);
            Context con = context.GetValue(this.Context);
            CRM4Connector conn = (CRM4Connector)con.Connector[Entity.System];

            try
            {
                if (!String.IsNullOrEmpty(Entity.Id))
                {
                    Event evnt = new Event(DateTime.Now, "Delete", Entity);
                    //con.BlockEvent(evnt);

                    conn.crmService.Delete(Entity.Name, new Guid(Entity.Id));
                }
            }
            catch (Exception ex)
            {
                throw new SystemOperationException(Entity, ex);
            }
        }
    }
}
